package com.explorex.infra.puma.gateway.netty.ws.message.handler;


import com.explorex.infra.puma.gateway.netty.common.utils.JacksonObjectMapper;
import com.explorex.infra.puma.gateway.netty.ws.WebSocketSession;
import com.explorex.infra.puma.gateway.netty.ws.enums.MessageTypeEnum;
import com.explorex.infra.puma.gateway.netty.ws.message.Message;
import com.explorex.infra.puma.gateway.netty.ws.message.MessageBody;
import com.explorex.infra.puma.gateway.netty.ws.message.MessageHeader;
import com.explorex.infra.puma.gateway.netty.ws.message.RespMessageBody;
import lombok.Data;

import java.io.Serializable;

/**
 * @author: JacceYang
 * @createTime: 2022/07/19 11:29 上午
 * @description: 用户状态变更消息处理器
 */
public class StatusChangeMessageProcessor implements BaseProcessor {
    @Override
    public short getMessageType() {
        return MessageTypeEnum.STATUS_CHANGE.getType();
    }

    @Override
    public Message process(WebSocketSession session, Message messageFrame) {
        StatusChangeMessage statusChangeMessage = (StatusChangeMessage) messageFrame.getBody();
        session.setStatus(statusChangeMessage.getStatus());
        MessageHeader header = new MessageHeader(MessageTypeEnum.STATUS_CHANGE_ACK.getType());
        header.trackHeader(messageFrame.getHeader());
        Message message = new Message();
        message.setHeader(header);
        return message;
    }

    @Override
    public MessageBody parseBody(int contentType, String msgBody) {
        return JacksonObjectMapper.loadObjectUnchecked(msgBody, StatusChangeMessage.class);
    }

    @Data
    static class StatusChangeMessage extends RespMessageBody implements Serializable {
        private int status;
    }
}
